package com.yiwei.zhh.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.yiwei.zhh.R;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.fragment.YouhuiTypeFragment;
import com.yiwei.zhh.view.MyViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/9/20.
 */

public class MyYouHuiAllActivity extends BaseActivity {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private Unbinder unbiner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_my_all_youhui);
        unbiner = ButterKnife.bind(this);
        baseInitView(this, "AboutActivity", Const.ViewType.ACTIVITY);
        init();
    }

    private void init() {
        setBackIntent();
        setTitleText("优惠券");
        YouhuiAdapter adapter = new YouhuiAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
//        viewPager.setOffscreenPageLimit(3);
        tabLayout.addTab(tabLayout.newTab().setText("未使用"));
        tabLayout.addTab(tabLayout.newTab().setText("使用记录"));
        tabLayout.addTab(tabLayout.newTab().setText("已过期"));
        tabLayout.getTabAt(0).select();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//
//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
//        tabLayout.setupWithViewPager(viewPager);
    }

    public void setTabText(int poi, int size) {
        switch (poi) {
            case 0:
                tabLayout.getTabAt(poi).setText("未使用 (" + size + ")");
                break;
            case 1:
                tabLayout.getTabAt(poi).setText("使用记录 (" + size + ")");
                break;
            case 2:
                tabLayout.getTabAt(poi).setText("已过期 (" + size + ")");
                break;
        }

    }

    public class YouhuiAdapter extends FragmentPagerAdapter {

//        String[] tabs = {"未还账单", "还款记录"};

        public YouhuiAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {
            YouhuiTypeFragment fragment = new YouhuiTypeFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type", position);
            fragment.setArguments(bundle);
            return fragment;
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
    }
}
